<template>
  <a-drawer v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
            :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
            :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
            :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">
    <!--表单开始-->

    <a-form ref="formRef" :model="formState" layout="vertical" :rules="RULES">

      <a-form-item label="任务标题" field="title">
        <a-input v-model="formState.title" allow-clear placeholder="请填写任务标题"/>
      </a-form-item>

      <a-form-item label="任务程序" field="app.id" :rules="RULES.app.id">
        <smart-dict v-model="formState.app.id" :url="SystemOptions.ScheduleAppOptions" placeholder="请选择要指定的任务程序"/>
      </a-form-item>

      <a-form-item label="CRON表达式" field="cron">
        <a-input v-model="formState.cron" allow-clear placeholder="请填写任务CRON表达式"/>
      </a-form-item>

      <a-form-item label="运行参数" field="params">
        <a-input v-model="formState.params" allow-clear placeholder="请填写任务运行参数"/>
      </a-form-item>


      <a-form-item label="任务运行状态" field="state">
        <smart-dict v-model="formState.state" :url="DictOptions.TaskState" is-radio placeholder="请选择要指定的任务程序"/>
      </a-form-item>



      <a-form-item label="任务备注" field="remark">
        <a-textarea v-model="formState.remark" allow-clear placeholder="请填写任务备注"/>
      </a-form-item>



    </a-form>
    <!--表单结束-->
  </a-drawer>
</template>

<script lang="ts" setup>
import {EditDrawer} from '@/libs/core'
import {TASK_EDIT} from "@/pages/modules/system/schedule/resource/form";
import {SystemHttp} from '../resource/http'
import {DictOptions, SystemOptions} from "@/libs/options";

const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])
const {BODY, RULES} = TASK_EDIT

const {addTask, updateTask} = SystemHttp
const config = {tplRecord: BODY, rawRecord: props.record, rowKey: 'id', emit}

const service = new EditDrawer(config).setAddRequest(addTask).setUpdateRequest(updateTask)
const {formRef, formState, LAYER, isUpdate, onCancel, onSubmit} = service
LAYER.title = isUpdate ? '修改任务' : '新建任务'
</script>
